package com.yuxin.tongyi.lsll.thread;

import com.yuxin.tongyi.lsll.common.Constants;
import com.yuxin.tongyi.lsll.pojo.EventInfos;
import com.yuxin.tongyi.lsll.sub.CaremaPhotoUploadActivity;
import com.yuxin.tongyi.lsll.util.FileTool;
import com.yuxin.tongyi.lsll.util.PubUtil;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

/**
 * @author ww
 * @date:2012-4-7 上午10:31:23
 * @Desc:  获取本地事件数据线程
 * @version :
 * 
 */
public class GetEventDataThread implements Runnable {

	private boolean readstate = false;
	private Context context;
	private Handler handler;
	
	public GetEventDataThread(Context context, Handler handler){
		this.context = context;
		this.handler = handler;
	}
	
	@Override
	public void run() {
		Message msg = handler.obtainMessage();
		try {
			while(!readstate){
				int eventState = PubUtil.getConfigValue(context, Constants.CFG_EVENT_STATE, 0);
				//判断class标识， 0：尚未读取完 继续循环，其他:读取SharedPreferences数据
				if(eventState == 0){
					readstate = false;
				}else{
					Bundle bundle = new Bundle();
					String info = PubUtil.getConfigValue(context, Constants.CFG_EVENT_DATA, "");
					if (info != null && !"".equals(info)) {
						Object obj = FileTool.getStringToObject(info);
						if(obj instanceof EventInfos){
							EventInfos eventInfos = (EventInfos) obj;
							bundle.putSerializable(Constants.STREAM_EVENT_OBJECT, eventInfos);
							msg.setData(bundle);
						}
					}
					if(eventState==Constants.TH_EVENT_EMPTY || eventState == Constants.TH_EVENT_ERROR || eventState ==Constants.TH_EVENT_OFF){
						eventState = Constants.TH_EVENT_OFF;
					}
					msg.what = eventState;
					readstate = true;
				}
			}
			handler.sendMessage(msg);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}
